//
//  UserInfoView.swift
//  willdo
//
//  Created by zz on 4/3/18.
//  Copyright © 2018 will do. All rights reserved.
//

import UIKit

class ExternalMemberView: UIView {

    let imgView = UIImageView()
    let nameLabel = UILabel()
    
    let positionImg = UIImageView()
    let positionView = UILabel()
    
    let telImg = UIImageView()
    let telView = UILabel()
    
    let noteImg = UIImageView()
    let noteView = UILabel()
    
    init(frame: CGRect, model: ExternalMemberModel) {
        super.init(frame: frame)
        self.backgroundColor = BACKGROUND_GRAY
        let topView = UIView()
        topView.backgroundColor = UIColor.white
        addSubview(topView)
        topView.backgroundColor = UIColor.white
        topView.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(180)
        }
        topView.addSubview(imgView)
        topView.addSubview(nameLabel)
        imgView.layer.cornerRadius = 45
        imgView.clipsToBounds = true
        imgView.image = UIImage(data: model.avatar)
        imgView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(25)
            make.centerX.equalToSuperview()
            make.height.width.equalTo(90)
        }
        nameLabel.text = model.name
        nameLabel.font = UIFont(name: "PingFang SC", size: 18)
        nameLabel.textColor = UIColor(hexString: "#496069")
        
        nameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(imgView.snp.bottom).offset(21)
            make.centerX.equalTo(imgView)
        }
        
        let posView = getCell(img: positionImg, label: positionView)
        addSubview(posView)
        positionImg.image = UIImage(named: "work_picture")
        positionView.text = model.position
        posView.snp.makeConstraints { (make) in
            make.top.equalTo(topView.snp.bottom).offset(20)
            make.left.right.equalToSuperview()
            make.height.equalTo(52)
        }
        let sepLine0 = UIView(frame: CGRect(x: 0, y: 0, width: 1, height: 1))
        addSubview(sepLine0)
        sepLine0.backgroundColor = BACKGROUND_GRAY
        sepLine0.snp.makeConstraints { (make) in
            make.width.left.equalToSuperview()
            make.top.equalTo(posView.snp.bottom)
            make.height.equalTo(1)
        }
        
        let phoneView = getCell(img: telImg, label: telView)
        addSubview(phoneView)
        telImg.image = UIImage(named: "contact_picture")
        telView.text = model.tel
        phoneView.snp.makeConstraints { (make) in
            make.top.equalTo(sepLine0.snp.bottom)
            make.left.right.equalToSuperview()
            make.height.equalTo(posView)
        }
        
        let sepLine1 = UIView(frame: CGRect(x: 0, y: 0, width: 1, height: 1))
        addSubview(sepLine1)
        sepLine1.backgroundColor = BACKGROUND_GRAY
        sepLine1.snp.makeConstraints { (make) in
            make.width.left.equalToSuperview()
            make.top.equalTo(phoneView.snp.bottom)
            make.height.equalTo(1)
        }
        let note = getCell(img: noteImg, label: noteView)
        addSubview(note)
        noteImg.image = UIImage(named: "remark_picture")
        noteView.text = model.note
        note.snp.makeConstraints { (make) in
            make.top.equalTo(sepLine1.snp.bottom)
            make.left.right.equalToSuperview()
            make.bottom.equalTo(noteView.snp.bottom).offset(12)
        }
    }
    
    private func getCell(img:UIView, label:UILabel) -> UIView{
        let cellView = UIView()
        addSubview(cellView)
        cellView.backgroundColor = UIColor.white
        cellView.addSubview(img)
        cellView.addSubview(label)
        img.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(17)
            make.left.equalToSuperview().offset(27)
            make.height.width.equalTo(18)
        }
        label.sizeToFit()
        label.font = UIFont(name: "PingFang SC", size: 15)
        label.textColor = UIColor(hexString: "#496069")
        label.numberOfLines = 0
        label.snp.makeConstraints { (make) in
            make.top.equalTo(img)
            make.left.equalTo(img.snp.right).offset(17)
        }
        return cellView
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
